package demo.influxdb.java;

import lombok.extern.slf4j.Slf4j;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.Point;
import org.influxdb.dto.Query;
import org.influxdb.dto.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author Administrator
 */
@Slf4j
@Component
public class InfluxDatabaseUtil {

    private String database;
    private String retentionPolicy;
    private String retentionPolicyTime;
    private InfluxDB influxdb;

    @Autowired
    private InfluxDatabaseConfig influxDatabaseConfig;

    @PostConstruct
    private void init() {

        // 保存策略
        if (StringUtils.isEmpty(influxDatabaseConfig.retentionPolicy)) {
            this.retentionPolicy = "autogen";
        } else {
            this.retentionPolicy = influxDatabaseConfig.retentionPolicy;
        }

        // 数据保存策略中数据保存时间
        if (StringUtils.isEmpty(influxDatabaseConfig.retentionPolicyTime)) {
            this.retentionPolicyTime = "30d";
        } else {
            this.retentionPolicyTime = influxDatabaseConfig.retentionPolicyTime;
        }

        if (StringUtils.isEmpty(influxDatabaseConfig.database)) {
            // 创建数据库并赋值
            System.out.println("创建数据库！");
        }
        this.database = influxDatabaseConfig.database;

        // 初始化 InfluxDB 实例
        initInfluxDatabase();

        // 数据库设置保存策略
        createRetentionPolicy();
    }

    /**
     * 初始化数据库连接
     */
    private void initInfluxDatabase() {
        influxdb = InfluxDBFactory.connect(influxDatabaseConfig.url, influxDatabaseConfig.userName, influxDatabaseConfig.password);
        influxdb.setDatabase(database);
    }

    /**
     * 设置数据保存策略
     * default 策略名
     * database 数据库名
     * 30d 数据保存时限30天
     * 1 副本个数为1
     * 结尾 DEFAULT 表示 设为默认的策略
     */
    private void createRetentionPolicy() {
        String command = String.format("CREATE RETENTION POLICY \"%s\" ON \"%s\" DURATION %s REPLICATION %s DEFAULT",
                retentionPolicy, database, retentionPolicyTime, 1);
        this.query(command);
    }

    /**
     * 查询
     *
     * @param command 查询语句
     * @return 查询结果
     */
    public QueryResult query(String command) {
        return influxdb.query(new Query(command, database));
    }

    /**
     * 插入
     *
     * @param measurement 表
     * @param tags        标签
     * @param fields      字段
     */
    public void insert(String measurement, Map<String, String> tags, Map<String, Object> fields) {
        Point.Builder builder = Point.measurement(measurement);
        builder.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS);
        builder.tag(tags);
        builder.fields(fields);
        log.info("influxDB insert data:[{}]", builder.build().toString());
        influxdb.write(database, "", builder.build());
    }
}
